package com.glela.controller;

import com.glela.live.model.RedPackage;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by jinweiwei on 2018/4/19.
 */
public class RandomRedPackage {

    public static List<Double> getRandomMoney(RedPackage _redPackage) {
        // remainSize 剩余的红包数量
        // remainMoney 剩余的钱
        List<Double> result = new ArrayList<>(_redPackage.getRemainSize());
        while (_redPackage.getRemainSize()>0){
            if (_redPackage.getRemainSize() == 1) {
                _redPackage.setRemainSize(_redPackage.getRemainSize()-1);
                result.add((double) Math.round(_redPackage.getRemainMoney() * 100) / 100);
            }else{
                Random r     = new Random();
                double min   = 0.01; //
                double max   = _redPackage.getRemainMoney() / _redPackage.getRemainSize() * 2;
                double money = r.nextDouble() * max;
                money = money <= min ? 0.01: money;
                money = Math.floor(money * 100) / 100;
                _redPackage.setRemainSize(_redPackage.getRemainSize()-1);
                _redPackage.setRemainMoney(_redPackage.getRemainMoney()-money);
                result.add(money);
            }
        }
        return result;
    }

    public static void main(String[] args) {
        RedPackage redPackage = new RedPackage(1500,111.3);
        if (redPackage.getRemainSize()*0.01>redPackage.getRemainMoney()){
            System.out.println("设定的总金额不足以支撑最低金额");
        }
//        for (int i = 0; i < count; i++) {
//            System.out.println(getRandomMoney(redPackage));
//        }
        List<Double> reds = getRandomMoney(redPackage);
        for (Double d: reds) {
            System.out.println(d);
        }
    }
}
